﻿using HttpBot;
using NPOI.ReadExcel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Threading;
using System.Web;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("程序版本:V1.0.3");
            var imgRootPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "images");

            if (!Directory.Exists(imgRootPath))
            {
                Directory.CreateDirectory(imgRootPath);
            }

            try
            {
                Start(imgRootPath);
            }
            catch (Exception ex)
            {
                File.AppendAllText("error.txt", ex.ToString() + "\r\n"); 
                WriteLogHelper.Exit("当前程序出现异常，日志已记录");
            }

            Thread.Sleep(10);
            WriteLogHelper.Exit("当前文档已经全部处理完成,请按任意键退出"); 
        }

        private static void Start(string imgRootPath)
        { 
            var web_args = File.ReadAllLines("参数.txt");
            var workbook = WorkbookFactory.Create("list.xlsx");
            var sheet = workbook.GetSheetAt(0);
            Console.WriteLine("一共: "+ sheet.LastRowNum+" 数据等待处理.");
            for (var j = sheet.FirstRowNum; j <= sheet.LastRowNum; j++)
            {
                var r = sheet.GetRow(j) as IRow;
                var name = r.Cells[0];
                var id = r.Cells[1]; 
                try
                {
                    Console.WriteLine("处理:第 " +(j+1) + " 行数据.");
                    var dic = new DicParam();
                    dic.Params.Add(new Model { Key = "businesscode", Value = "01" });
                    dic.Params.Add(new Model { Key = "certno", Value = HttpUtility.UrlEncode(id.ToString(),Encoding.GetEncoding("gb2312")) });
                    dic.Params.Add(new Model { Key = "certname", Value = HttpUtility.UrlEncode(name.ToString(),Encoding.GetEncoding("gb2312")) }); 
                    for (int i = 0; i < 4; i++)
                    {
                        dic.Params.Add(new Model { Key = "certno", Value = HttpUtility.UrlEncode(string.Empty) });
                        dic.Params.Add(new Model { Key = "certname", Value = HttpUtility.UrlEncode(string.Empty) });
                    }

                    //读取地址
                    var url = string.Format(web_args[0], (web_args[1].Split(';')[1]).Split('=')[1]); //将请求地址对应的cookie存上去
                    PictureDownloadBot.Download(url,web_args[1], imgRootPath,dic,r); 
                }
                catch (Exception ex)
                {
                    r.CreateCell(5).SetCellValue("失败");
                    Console.WriteLine(" * "+name+"尝试下载照片失败");
                    File.AppendAllText("error.txt", ex.ToString() + "\r\n");
                } 
            }

            var len = Directory.GetFiles("images").Length;
            Console.WriteLine(string.Format("数据共:{1}个用户的信息等待处理,成功处理:{0}个", sheet.LastRowNum,len)); 
            using (var fs = new FileStream("new_list.xlsx", FileMode.Create))
            {
                workbook.Write(fs);
            } 
            workbook.Close();
        }
    }
}